package com.qire.manhua.dal.service;

import com.qire.antsbinder.dal.DataService;
import com.qire.antsbinder.dal.annotation.StrategyAnnotation;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.collect.ArrayUtil;
import com.qire.common.constant.Url;
import com.qire.common.dal.WarehouseFactory;
import com.qire.manhua.dal.ITradingDataDAL;
import com.qire.manhua.model.viewModel.impl.AccountPurchaseHistoryViewModel;
import com.qire.manhua.model.vo.PurchaseHistoryVO;

public class AccountPurchaseHistoryService extends DataService {

    private final ITradingDataDAL tradingDataDAL = WarehouseFactory.create(this, ITradingDataDAL.class);

    private final AccountPurchaseHistoryViewModel viewModel;

    public AccountPurchaseHistoryService(AccountPurchaseHistoryViewModel viewModel) {
        this.viewModel = viewModel;
    }

    public void fetchPurchaseHistoryData(int pageNo) {
        tradingDataDAL.fetchPurchaseHistoryData(pageNo);
    }
    @StrategyAnnotation(event = Url.PurchaseHistoryData)
    public void fetchPurchaseHistoryDataApiCallback(PurchaseHistoryVO[] dataVOs, boolean isCache) {

        viewModel.refreshLayoutFinish(300);
        if(ObjectUtil.isNull(dataVOs)) {
            return;
        }

        if(ArrayUtil.isEmpty(dataVOs)) {
            viewModel.noMoreDataWasShown();
            return;
        }

        for(PurchaseHistoryVO vo : dataVOs) {
            int comicId = vo.book.book_id;
            String comicName = vo.book.book_name;
            String comicCover = vo.book.book_cover;
            CharSequence coinUnLockInfo = String.format("永久解锁%s话", vo.unlock.forever);
            CharSequence beanUnLockInfo = String.format("3日解锁%s话（%s话已过期）", vo.unlock.moment, vo.unlock.invalidCount());
            CharSequence updateInfo = String.format("更新至%s话", vo.book.last_update_px);
            viewModel.addPurchaseHistoryItem(comicId, comicName, comicCover, coinUnLockInfo, beanUnLockInfo, updateInfo);
        }

        viewModel.refreshPurchaseHistoryAdapter();
        viewModel.refreshEmptyDataPanel();

        if(!isCache) {
            viewModel.nextPage();
        }
    }

}
